#!/bin/sh

# craftmesh.sh v0.2 2013-09-08
# Flavio Bertini fl.bertini@gmail.com

# Start Craftmesh
# Help
if [ $# = 0 ]; then
	echo "Usage: craftmesh [OPTIONS | FILE]"
	echo "Try './craftmesh -h' for more information."

# Usage
elif [ $1 = "-h" ]; then
	echo "./craftmesh start            - to start leap service"
	echo "./craftmesh stop             - to stop leap service"
	echo "./craftmesh make             - to compile the project"
	echo "./craftmesh run filenames    - to run the project"
   	echo "./craftmesh remake filenames - to compile and run the project"
	echo "./craftmesh clean            - to clean the directory"

# Start LEAP service
elif [ $1 = "start" ]; then
	if [ -z "$(pgrep leapd)" ]; then
		leapd > leaplog.txt &
	fi
	if [ -z "$(pgrep LeapControlPane)" ]; then
		LeapControlPanel &
	fi

# Stop LEAP service
elif [ $1 = "stop" ]; then
	sudo kill -9 `pgrep leapd`
	sudo kill -9 `pgrep LeapControlPane`     # this is the process used name

# Make
elif [ $1 = "make" ]; then
	make

# Make clean
elif [ $1 = "clean" ]; then
	make clean

# Make and Run
elif [ $1 = "remake" ]; then
	shift
	make && export LD_LIBRARY_PATH=/usr/lib/Leap/ && ./Craftmesh $@

# Run
elif [ $1 = "run" ]; then
	shift
	export LD_LIBRARY_PATH=/usr/lib/Leap/ && ./Craftmesh $@

# What?
else
	echo "Learn to write!!"

fi
